package com.comdo.newup_loan.module.home;

import android.Manifest;
import android.app.Dialog;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.comdo.newup_loan.R;
import com.comdo.newup_loan.base.BaseFragment;
import com.comdo.newup_loan.comom.Config;
import com.comdo.newup_loan.comom.GlideImageLoader;
import com.comdo.newup_loan.http.ApiService;
import com.comdo.newup_loan.model.HomeDataBean;
import com.comdo.newup_loan.model.NewUpVersionBean;
import com.comdo.newup_loan.model.PicsBean;
import com.comdo.newup_loan.module.limit.CarLimitActivity;
import com.comdo.newup_loan.module.notice.NoticeListActivity;
import com.comdo.newup_loan.module.store.StoreListActivity;
import com.comdo.newup_loan.util.update.ClientUpdate;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import kr.co.namee.permissiongen.PermissionGen;

/**
 * Created by liyuan on 2017/9/5.
 */

public class HomeFragment extends BaseFragment<HomePresenter> implements HomeContract.View, OnBannerListener, View.OnClickListener {
    @BindView(R.id.main_banner)
    Banner mBanner;
    @BindView(R.id.img_call)
    ImageView callImageView;
    @BindView(R.id.img_suggest)
    ImageView suggestImageView;
    @BindView(R.id.tv_notice)
    TextView noticeTextView;
    @BindView(R.id.tv_storeinfo)
    TextView storeTextView;
    @BindView(R.id.tv_repay_ability)
    TextView repayTextView;
    @BindView(R.id.tv_car_limit)
    TextView carLimitTextView;
    @BindView(R.id.tv_flow)
    TextView flowTextView;
    @BindView(R.id.img_want_loan)
    ImageView wantLoanImageView;
    @BindView(R.id.img_find1)
    ImageView find1ImageView;
    @BindView(R.id.img_find2)
    ImageView find2ImageView;
    @BindView(R.id.img_find3)
    ImageView find3ImageView;
    private Intent intent;
    private HomeDataBean homeData;
    private String url;
    private int currentVersionCode;
    @Override
    protected int getContentViewLayoutID() {
        return R.layout.fragment_home;
    }

    @Override
    protected void init() {
        mPresenter.getBannerData();
        PackageManager manager = getActivity().getPackageManager();
        try {
            PackageInfo info = manager.getPackageInfo(getActivity().getPackageName(), 0);
            String appVersion = info.versionName; // 版本名
            // 版本号
            currentVersionCode = info.versionCode;;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        mPresenter.getVersion(0);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        mBanner.setIndicatorGravity(BannerConfig.RIGHT);
        mBanner.setOnBannerListener(this);
        suggestImageView.setOnClickListener(this);
        callImageView.setOnClickListener(this);
        noticeTextView.setOnClickListener(this);
        storeTextView.setOnClickListener(this);
        repayTextView.setOnClickListener(this);
        carLimitTextView.setOnClickListener(this);
        flowTextView.setOnClickListener(this);
        wantLoanImageView.setOnClickListener(this);
        find1ImageView.setOnClickListener(this);
        find2ImageView.setOnClickListener(this);
        find3ImageView.setOnClickListener(this);
    }

    @Override
    public HomePresenter getmPresenter() {
        return new HomePresenter();
    }

    @Override
    public void presenterSetView() {
        if (mPresenter != null)
            mPresenter.setView(this);
    }

    @Override
    public void showBannerFail(String failMessage) {
        showToast(failMessage);
    }

    @Override
    public void setBanner(HomeDataBean homeDataBean) {
        this.homeData = homeDataBean;
        //设置轮播图
        List<String> Urls = new ArrayList<>();
        for (PicsBean bean : homeData.getPicInfo()) {
            String url = Config.PIC_FORE + bean.getPicUrl();
            Urls.add(url);
        }
        mBanner.setImages(Urls).setImageLoader(new GlideImageLoader()).start();

        noticeTextView.setText(homeData.getFirstNoticeTitle());
        Config.registrationProtocol=homeDataBean.getRegistrationProtocol();
    }

    @Override
    public void OnBannerClick(int position) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.img_suggest:
                //意见反馈
                toSuggestActivity();
                break;
            //拨打电话
            case R.id.img_call:
                getCallDialg();
                break;
            //公告
            case R.id.tv_notice:
                toNoticeActivity();
                break;
            //门店情况
            case R.id.tv_storeinfo:
                toStoreActivity();
                break;
            //还款能力评估
            case R.id.tv_repay_ability:
                toAssessActivity();
                break;
            //车辆额度查询
            case R.id.tv_car_limit:
                toLimitActivity();
                break;
            //借款流程
            case R.id.tv_flow:
                toLoanProcessActivity();
                break;
            //我要借款
            case R.id.img_want_loan:
                toLoanActivity();
                break;
            //发现更多1
            case R.id.img_find1:
                if (homeData != null) {
                    url = homeData.getFindMore().get(0).getPicUrl();
                    toFindMore(url,"车贷业务介绍");
                }
                break;
            //发现更多2
            case R.id.img_find2:
                if (homeData != null) {
                    url = homeData.getFindMore().get(1).getPicUrl();
                    toFindMore(url,"常见问题");
                }
                break;
            //发现更多3
            case R.id.img_find3:
                if (homeData != null) {
                    url = homeData.getFindMore().get(2).getPicUrl();
                    toFindMore(url,"关于新升贷");
                }
                break;

        }
    }

    private void toSuggestActivity() {
        intent = new Intent(getActivity(), SuggestionActivity.class);
        startActivity(intent);
    }

    private void toNoticeActivity() {
        intent = new Intent(getActivity(), NoticeListActivity.class);
        startActivity(intent);
    }

    private void toLoanActivity() {
        if(Config.loginInfo==null){
            showToast("请先登录!");
            return;
        }
        intent = new Intent(getActivity(), WantLoanActivity.class);
        startActivity(intent);
    }

    private void toStoreActivity() {
        intent = new Intent(getActivity(), StoreListActivity.class);
        startActivity(intent);
    }

    private void toLimitActivity() {
        intent = new Intent(getActivity(), CarLimitActivity.class);
        startActivity(intent);
    }
    private void toAssessActivity() {
        intent = new Intent(getActivity(), AssessmentActivity.class);
        startActivity(intent);
    }
    private void toLoanProcessActivity() {
        intent = new Intent(getActivity(), ADWebviewActivity.class);
        intent.putExtra("title", "借款流程");
        intent.putExtra("url", ApiService.PATHS+homeData.getLoanProcess());
        startActivity(intent);
    }

    private void toFindMore(String url,String title) {
        intent = new Intent(getActivity(), ADWebviewActivity.class);
        intent.putExtra("title", title);
        intent.putExtra("url", ApiService.PATHS+url);
        startActivity(intent);
    }
    private void requestPermiss(){
        PermissionGen.with(getActivity())
                .addRequestCode(100)
                .permissions(
                        Manifest.permission.CALL_PHONE)
                .request();
    }
    public Dialog getCallDialg() {
        requestPermiss();
        final Dialog dialog = new Dialog(getActivity(), R.style.Dialog_Fullscreen);
        dialog.show();
        dialog.setCancelable(true);
        Window window = dialog.getWindow();
        window.setContentView(R.layout.dialog_call);
        window.setGravity(Gravity.BOTTOM);
        Button dialog_no = (Button) window.findViewById(R.id.bt_qx);
        Button dialog_phone = (Button) window.findViewById(R.id.bt_phone);
        Button dialog_phone1 = (Button) window.findViewById(R.id.bt_phone1);
        dialog_no.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                dialog.dismiss();

            }
        });
        dialog_phone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                dialog.dismiss();
                // 用intent启动拨打电话
                Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"
                        + "4000066571"));
                startActivity(intent);

            }
        });
        dialog_phone1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                dialog.dismiss();
                // 用intent启动拨打电话
                Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"
                        + "4008260773"));
                startActivity(intent);

            }
        });
        return dialog;
    }

    @Override
    public void getVersion(NewUpVersionBean result) {
        if (currentVersionCode < result
                .getVersion()) {
            // 调取版本更新入口
            new ClientUpdate(getActivity(),
                    result.getUrl(), result
                    .getContent(), result
                    .getVersionName()).check();

            Log.e("appVersion", "自己版本:"
                    + currentVersionCode);
            Log.e("appVersion",
                    "得到版本:" + result.getVersion());
            Log.e("appVersion",
                    "版本网址：" + result.getUrl());
        } else {
        }
    }

    @Override
    public void getVersionFail(String failMessage) {
    }


}
